package pe.gob.sedapal.scmultimedia.rcp.view.estadocuenta;

import java.util.ArrayList;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.wb.swt.ResourceManager;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;

import pe.gob.sedapal.scmultimedia.config.ApplicationContext;
import pe.gob.sedapal.scmultimedia.domain.Suministro;
import pe.gob.sedapal.scmultimedia.service.EstadoCuentaService;
import pe.gob.sedapal.scmultimedia.sessionbean.DataSessionBean;
import pe.gob.sedapal.scmultimedia.util.Constante;
import pe.gob.sedapal.scmultimedia.util.Messages;
import pe.gob.sedapal.scmultimedia.wb.swt.SWTResourceManager;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.MouseTrackAdapter;

public class SuministrosPorNumeroView extends ViewPart {
	
	public static final String ID = "scmultimedia.consultaEstadoCuentaView.buscarDireccionView.SuministrosPorNumeroView";
	
	private TableViewer viewer;

	private EstadoCuentaService estadoCuentaService;
	private DataSessionBean dataSessionBean;
	
	private ArrayList<Suministro> suministros = new ArrayList<Suministro>();
	private Text txtFieldProvincia;
	private Text txtFieldDistrito;
	private Text txtFieldLocalidad;
	private Text txtFieldNumero;
	private Text txtFieldCalle;
	private Text txtFieldNis;
	
	public SuministrosPorNumeroView() {
		estadoCuentaService = (EstadoCuentaService)ApplicationContext.getBean(EstadoCuentaService.class);
		dataSessionBean = (DataSessionBean)ApplicationContext.getBean(DataSessionBean.class);
		dataSessionBean.setSuministro(null);
	}

	@Override
	public void createPartControl(final Composite parent) {
		parent.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Ubuntu", 12, SWT.BOLD));
		parent.setLayout(null);
		parent.setBackgroundImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_FONDO_SEDAPAL));
		final Label lblBack = new Label(parent, SWT.NONE);
		lblBack.addMouseTrackListener(new MouseTrackAdapter() {
	    	@Override
	    	public void mouseEnter(MouseEvent e) {
	    		lblBack.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_REGRESAR_ON));
	    	}
	    	@Override
	    	public void mouseExit(MouseEvent e) {
	    		lblBack.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_REGRESAR_OFF));
	    	}
	    });  		
		lblBack.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				IWorkbenchPage pagina = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				IViewReference vistaActual = pagina.findViewReference(SuministrosPorNumeroView.ID);
				
				pagina.hideView(vistaActual);
				
				try{
					pagina.showView(BuscarDireccionView.ID);
				}catch(Exception ex){
					ex.printStackTrace();
				}
			}
		});
		lblBack.setBounds(577, 447, 169, 58);
		lblBack.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_REGRESAR_OFF));
		
		Label lblProvincia = new Label(parent, SWT.NONE);
		lblProvincia.setForeground(SWTResourceManager.getColor(Constante.VALOR_FUENTE_RED,Constante.VALOR_FUENTE_GREEN,Constante.VALOR_FUENTE_BLUE));
		lblProvincia.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Ubuntu", 13, SWT.BOLD));
		lblProvincia.setBounds(30, 91, 81, 27);
		lblProvincia.setText(Messages.suministroPorNumero_etiqueta_provincia);
		
		Label lblDistrito = new Label(parent, SWT.NONE);
		lblDistrito.setForeground(SWTResourceManager.getColor(Constante.VALOR_FUENTE_RED,Constante.VALOR_FUENTE_GREEN,Constante.VALOR_FUENTE_BLUE));
		lblDistrito.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Ubuntu", 13, SWT.BOLD));
		lblDistrito.setBounds(30, 124, 70, 27);
		lblDistrito.setText(Messages.suministroPorNumero_etiqueta_distrito);
		
		Label lblLocalidad = new Label(parent, SWT.NONE);
		lblLocalidad.setForeground(SWTResourceManager.getColor(Constante.VALOR_FUENTE_RED,Constante.VALOR_FUENTE_GREEN,Constante.VALOR_FUENTE_BLUE));
		lblLocalidad.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Ubuntu", 13, SWT.BOLD));
		lblLocalidad.setBounds(30, 157, 81, 27);
		lblLocalidad.setText(Messages.suministroPorNumero_etiqueta_localidad);
		
		Label lblCalle = new Label(parent, SWT.NONE);
		lblCalle.setForeground(SWTResourceManager.getColor(Constante.VALOR_FUENTE_RED,Constante.VALOR_FUENTE_GREEN,Constante.VALOR_FUENTE_BLUE));
		lblCalle.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Ubuntu", 13, SWT.BOLD));
		lblCalle.setBounds(30, 190, 50, 27);
		lblCalle.setText(Messages.suministroPorNumero_etiqueta_calle);
		
		Label lblNumero = new Label(parent, SWT.NONE);
		lblNumero.setForeground(SWTResourceManager.getColor(Constante.VALOR_FUENTE_RED,Constante.VALOR_FUENTE_GREEN,Constante.VALOR_FUENTE_BLUE));
		lblNumero.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Ubuntu", 13, SWT.BOLD));
		lblNumero.setBounds(482, 190, 70, 27);
		lblNumero.setText(Messages.suministroPorNumero_etiqueta_numero);
		
		Label lblNumeroDeSuministro = new Label(parent, SWT.NONE);
		lblNumeroDeSuministro.setForeground(SWTResourceManager.getColor(Constante.VALOR_FUENTE_RED,Constante.VALOR_FUENTE_GREEN,Constante.VALOR_FUENTE_BLUE));
		lblNumeroDeSuministro.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Ubuntu", 13, SWT.BOLD));
		lblNumeroDeSuministro.setBounds(30, 447, 108, 27);
		lblNumeroDeSuministro.setText(Messages.suministroPorNumero_etiqueta_suministro);
		
		viewer = new TableViewer(parent);

		viewer.setContentProvider(ArrayContentProvider.getInstance());
		
		final Table table = viewer.getTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				if(table.getSelectionIndex() > -1){
					int suministroSeleccionado = table.getSelectionIndex();
		    		dataSessionBean.setSuministro(suministros.get(suministroSeleccionado));	
		    		txtFieldNis.setText(dataSessionBean.getSuministro().getNis().toString());
				}
			}
		});
		table.setSize(716, 208);
		table.setLocation(30, 223);
		viewer.getTable().setHeaderVisible(true);
		viewer.getTable().setLinesVisible(true);
		
		TableViewerColumn viewerColumnNis = new TableViewerColumn(viewer, SWT.NONE);
		TableColumn columnaNis = viewerColumnNis.getColumn();
		columnaNis.setWidth(80);
		columnaNis.setText(Messages.suministroPorNumero_tabla_nis);
		viewerColumnNis.setLabelProvider(new ColumnLabelProvider(){
			@Override
			public String getText(Object element) {
				Suministro suministro = (Suministro) element;
				return suministro.getNis().toString();
			}			
		});
		
		TableViewerColumn viewerColumnPiso = new TableViewerColumn(viewer, SWT.NONE);
		TableColumn columnaPiso = viewerColumnPiso.getColumn();
		columnaPiso.setWidth(80);
		columnaPiso.setText(Messages.suministroPorNumero_tabla_pisoDpto);
		viewerColumnPiso.setLabelProvider(new ColumnLabelProvider(){
			@Override
			public String getText(Object element) {
				Suministro suministro = (Suministro) element;
				return suministro.getInterior();
			}			
		});
		
		TableViewerColumn viewerColumnNombre = new TableViewerColumn(viewer, SWT.NONE);
		TableColumn columnaNombre = viewerColumnNombre.getColumn();
		columnaNombre.setWidth(100);
		columnaNombre.setText(Messages.suministroPorNumero_tabla_nombre);
		viewerColumnNombre.setLabelProvider(new ColumnLabelProvider(){
			@Override
			public String getText(Object element) {
				Suministro suministro = (Suministro) element;
				return suministro.getApNombres();
			}			
		});
		
		/*viewer.setLabelProvider(new ColumnLabelProvider() {
				@Override
				public String getText(Object element) {
					Suministro suministro = (Suministro) element;
					return suministro.getNis().toString();
				}
			});
		*/
		/*suministros = (ArrayList<Suministro>) estadoCuentaService.listarSuministrosPorNumero(dataSessionBean.getNumero().getNif());
		for (Suministro suministro : suministros) {
			viewer.add(suministro);			
		}	*/
		final Label lblAceptar = new Label(parent, SWT.NONE);
		lblAceptar.addMouseTrackListener(new MouseTrackAdapter() {
	    	@Override
	    	public void mouseEnter(MouseEvent e) {
	    		lblAceptar.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_ACEPTAR_ON));
	    	}
	    	@Override
	    	public void mouseExit(MouseEvent e) {
	    		lblAceptar.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_ACEPTAR_OFF));
	    	}
	    });		
		lblAceptar.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {

				if(dataSessionBean.getSuministro() != null){
		    		IWorkbenchPage pagina = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
					IViewReference vistaActual = pagina.findViewReference(SuministrosPorNumeroView.ID);
					
					pagina.hideView(vistaActual);
					
					try{
						pagina.showView(ResultadoView.ID);
					}catch(Exception ex){
						ex.printStackTrace();
					}
				}else{
					MessageDialog.openInformation(parent.getShell(), Constante.MSG_ERROR, Constante.MSG_ERROR_NUMERO_DE_SUMINISTRO_NO_SELECCIONADO);
				}

			}
		});
		lblAceptar.setBounds(383, 447, 169, 58);
		lblAceptar.setImage(org.eclipse.wb.swt.ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_BOTON_ACEPTAR_OFF));

		Label label_3 = new Label(parent, SWT.NONE);
		label_3.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_LOGO_SEDAPAL_INTERNO));
		label_3.setBounds(635, 10, 192, 84);		
		
		Label label = new Label(parent, SWT.NONE);
		label.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_BARRA));
		label.setBounds(10, 61, 736, 8);
		
		Label label_1 = new Label(parent, SWT.NONE);
		label_1.setText(Messages.suministroPorNumero_titulo);
		label_1.setForeground(org.eclipse.wb.swt.SWTResourceManager.getColor(51, 102, 153));
		label_1.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("FreeSans", 17, SWT.BOLD | SWT.ITALIC));
		label_1.setBounds(45, 25, 267, 30);
		
		Label label_2 = new Label(parent, SWT.NONE);
		label_2.setText(Messages.suministroPorNumero_subtitulo);
		label_2.setForeground(org.eclipse.wb.swt.SWTResourceManager.getColor(51, 102, 153));
		label_2.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("FreeSans", 12, SWT.BOLD | SWT.ITALIC));
		label_2.setBounds(318, 38, 252, 17);
		
	
		txtFieldProvincia = new Text(parent, SWT.BORDER);
		txtFieldProvincia.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Ubuntu", 12, SWT.NORMAL));
		txtFieldProvincia.setEditable(false);
		txtFieldProvincia.setBounds(128, 91, 210, 27);
		txtFieldProvincia.setText(dataSessionBean.getProvincia().getNomDepto());
		
		txtFieldDistrito = new Text(parent, SWT.BORDER);
		txtFieldDistrito.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Ubuntu", 12, SWT.NORMAL));
		txtFieldDistrito.setEditable(false);
		txtFieldDistrito.setBounds(128, 124, 279, 27);
		txtFieldDistrito.setText(dataSessionBean.getDistrito().getNomMunic());
		
		txtFieldLocalidad = new Text(parent, SWT.BORDER);
		txtFieldLocalidad.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Ubuntu", 12, SWT.NORMAL));
		txtFieldLocalidad.setEditable(false);
		txtFieldLocalidad.setBounds(128, 157, 279, 27);
		txtFieldLocalidad.setText(dataSessionBean.getLocalidad().getNomLocal());
		
		txtFieldNumero = new Text(parent, SWT.BORDER);
		txtFieldNumero.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Ubuntu", 12, SWT.NORMAL));
		txtFieldNumero.setEditable(false);
		txtFieldNumero.setBounds(560, 190, 108, 27);
		txtFieldNumero.setText(dataSessionBean.getNumero().getNumPuerta().toString());
		
		txtFieldCalle = new Text(parent, SWT.BORDER);
		txtFieldCalle.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Ubuntu", 12, SWT.NORMAL));
		txtFieldCalle.setEditable(false);
		txtFieldCalle.setBounds(128, 190, 333, 27);
		txtFieldCalle.setText(dataSessionBean.getCalle().getNomCalle());
		
		txtFieldNis = new Text(parent, SWT.BORDER);
		txtFieldNis.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Ubuntu", 12, SWT.NORMAL));
		txtFieldNis.setEditable(false);
		txtFieldNis.setBounds(144, 447, 142, 27);
		
		suministros = (ArrayList<Suministro>) estadoCuentaService.listarSuministrosPorNumero(dataSessionBean.getNumero().getNif());		
		viewer.setInput(suministros);
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub
	}
	
	public void crearColumnas(){
		
	}
}
